using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

using System.Globalization;
using System.Resources;

namespace HostMonitor
{
    public partial class MainFrm : Form
    {
        private string XmlPath;
        private Checker _checker;
        private ResourceManager _rm;

        public MainFrm()
        {
            InitializeComponent();

            _rm = new ResourceManager("HostMonitor.Resources", System.Reflection.Assembly.GetExecutingAssembly());

            //assign events to dataset
            hosts.Groups.TableNewRow += new DataTableNewRowEventHandler(Groups_TableNewRow);
            hosts.Groups.GroupsRowChanged += new HostsData.GroupsRowChangeEventHandler(Groups_GroupsRowChanged);
            hosts.Groups.GroupsRowDeleted += new HostsData.GroupsRowChangeEventHandler(Groups_GroupsRowDeleted);

            hosts.Hosts.TableNewRow += new DataTableNewRowEventHandler(Hosts_TableNewRow);
            hosts.Hosts.HostsRowChanged += new HostsData.HostsRowChangeEventHandler(Hosts_HostsRowChanged);

#if DEBUG
            XmlPath = Path.GetDirectoryName(Path.GetDirectoryName(Application.ExecutablePath));
            XmlPath = Path.Combine(Path.GetDirectoryName(XmlPath), "hosts.xml");             
#else
            XmlPath=Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "hosts.xml");
#endif   
            hosts.ReadXml(XmlPath);

            DataRelation rel = new DataRelation("HostsInGroup", hosts.Groups.Columns["GroupName"], hosts.Hosts.Columns["Group"]);
            hosts.Relations.Add(rel);            

            LoadData();
            
            //start pinging
            _checker = new Checker(comps, this);
            _checker.Start();
        }

        void Hosts_HostsRowChanged(object sender, HostsData.HostsRowChangeEvent e)
        {
            HostsData.HostsRow row = (HostsData.HostsRow)e.Row;
            foreach (ListViewItem item in comps.Items)
            {                
                if (item.Tag == row)
                {
                    item.Text = row.HostName;
                    item.SubItems[2].Text = row.Description;
                    if ((item.Group == null) || (item.Group.Header != row.Group))
                    {
                        foreach (ListViewGroup gr in comps.Groups)
                        {
                            if (gr.Header == row.Group)
                            {
                                item.Group = gr;
                                break;
                            }
                        }
                    }
                    break;
                }
            }
        }

        void Hosts_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            HostsData.HostsRow row = (HostsData.HostsRow)e.Row;
            ListViewGroup group = null;
            if (row.Group!="")
            {
                foreach (ListViewGroup gr in comps.Groups)
                {
                    if (gr.Header == row.Group)
                    {
                        group = gr;
                        break;
                    }
                } 
            }
            
            ListViewItem item = new ListViewItem(group);
            item.Text = row.HostName;
            item.Tag = row;
            item.SubItems.Add("");
            item.SubItems.Add(row.Description);
            comps.Items.Add(item);
        }

        void Groups_GroupsRowDeleted(object sender, HostsData.GroupsRowChangeEvent e)
        {
            HostsData.GroupsRow row = (HostsData.GroupsRow)e.Row;
            foreach (ListViewGroup gr in comps.Groups)
            {
                if (gr.Tag == row)
                {
                    comps.Groups.Remove(gr);
                    return;
                }
            }
        }

        void Groups_GroupsRowChanged(object sender, HostsData.GroupsRowChangeEvent e)
        {
            HostsData.GroupsRow row = (HostsData.GroupsRow)e.Row;
            foreach (ListViewGroup gr in comps.Groups)
            {
                if (gr.Tag == row)
                {
                    gr.Header = row.GroupName;
                    return;
                }
            }
        }

        void Groups_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {            
            ListViewGroup gr = new ListViewGroup();
            HostsData.GroupsRow row = (HostsData.GroupsRow)e.Row;
            gr.Header = row.GroupName;
            gr.Tag = row;
            comps.Groups.Add(gr);
        }

        private void LoadData()
        {
            comps.Items.Clear();
            comps.Groups.Clear();
           
            foreach (HostsData.GroupsRow group in hosts.Groups)
            {
                ListViewGroup gr = new ListViewGroup();
                gr.Header = group.GroupName;
                gr.Tag = group;
                comps.Groups.Add(gr);                
                foreach (HostsData.HostsRow host in group.GetChildRows("HostsInGroup"))
                {
                    ListViewItem ht = new ListViewItem(gr);
                    ht.Text = host.HostName;
                    ht.SubItems.Add("---");
                    ht.SubItems.Add(host.Description);
                    ht.Tag = host;
                    comps.Items.Add(ht);
                }
            }
        }

        private void btnGroups_Click(object sender, EventArgs e)
        {
            GroupsFrm.OpenDialog(hosts);
        }

        private void MainFrm_FormClosed(object sender, FormClosedEventArgs e)
        {
            _checker.Stop();
            hosts.WriteXml(XmlPath);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            NewHostFrm.OpenDialog(hosts);
        }

        private void comps_DoubleClick(object sender, EventArgs e)
        {
            if (comps.FocusedItem != null)
            {
                HostsData.HostsRow row = (HostsData.HostsRow)comps.FocusedItem.Tag;
                EditHostFrm.OpenDialog(row);
            }
        }

        private void comps_KeyUp(object sender, KeyEventArgs e)
        {
            if (comps.FocusedItem != null)
            {
                if (e.KeyCode == Keys.Delete)
                {
                    string str = _rm.GetString("HostDeleteQuestion");
                    str = string.Format(str, comps.FocusedItem.Text);
                    if (MessageBox.Show(str, _rm.GetString("HostDeleteTitle"), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        HostsData.HostsRow row = (HostsData.HostsRow)comps.FocusedItem.Tag;
                        row.Delete();
                        comps.FocusedItem.Remove();
                    }
                }
            }
        }

        private void btnSettings_Click(object sender, EventArgs e)
        {
            SettingsFrm.OpenDialog();
        }
    }
}